/*
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.downloads;

import static org.tiberiumlabs.lailaps.downloads.DownloadManager.LOG;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import org.tiberiumlabs.lailaps.list.StatusConstants;
import org.tiberiumlabs.lailaps.settings.Settings;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class DownloadSegment implements Runnable, StatusConstants {

    private ProtocolFacade protocolFacade;
    private InputStream inputStream;
    private FileChannel fileChannel;
    private long length;
    private int needStatus;
    private long offset;
    private long bytesWritten = 0;

    public DownloadSegment(ProtocolFacade protocolFacade, FileChannel fileChannel, long offset, long length) {
        this.protocolFacade = protocolFacade;
        this.fileChannel = fileChannel;
        this.offset = offset;
        this.length = length;
    }

    public void run() {
        try {
            connect();
            performDownload();
        } catch (Exception ex) {
            LOG.warn("DownloadSetgment exception", ex);
        }
    }

    protected void connect() {
        // TODO implement
    }

    protected void performDownload() throws IOException {
        int bufferSize = Settings.getNetworkSettings().getDownloadBufferSize();
        ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize);
        byte[] buffer = new byte[bufferSize];
        int read = 0;
        while (needStatus == WORKING && bytesWritten < length && (read = inputStream.read(buffer)) >= 0) {
            if (bytesWritten + read > length) {
                read = (int)(length - bytesWritten);
            }
            byteBuffer.put(buffer, 0, read);
            byteBuffer.flip();
            fileChannel.write(byteBuffer, offset);

            // clearing buffers and adding size to offset
            byteBuffer.clear();
            offset = offset + read;
            bytesWritten = bytesWritten + read;
        }
        done();
    }

    private void done() {
        protocolFacade.close();
        synchronized (this) {
            notify();
        }
    }

    // <editor-fold defaultstate="collapsed" desc=" setters and getters ">

    public long getBytesWritten() {
        return bytesWritten;
    }

    public void setFileChannel(FileChannel fileChannel) {
        this.fileChannel = fileChannel;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public void setLength(long length) {
        this.length = length;
    }

    public void setNeedStatus(int needStatus) {
        this.needStatus = needStatus;
    }

    public void setOffset(long offset) {
        this.offset = offset;
    }

    // </editor-fold>

}
